<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mycommon {
	function getSaveSqlStr($str)
	{
		if(get_magic_quotes_gpc())
		{
			return $str;
		}
		else
		{
			//return mysql_real_escape_string($str);
			$CI = &get_instance();
			return mysqli_real_escape_string($CI->db->conn_id, $str);
		}
	}
	
	function getCurrentUrl() 
	{		
		$pageURL = 'http';
		if ( isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
			$pageURL .= "://";
		$pageURL .= $_SERVER['HTTP_HOST'] .$_SERVER["REQUEST_URI"];
		return $pageURL;
	}
	
	function isUrl($url)
	{
		return (preg_match('/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? true : false;
	}
	
	function getLargeThumbPath($thumbPath)
	{
		if($thumbPath == '')
		 	return $thumbPath;
		$pos = strripos($thumbPath, '.');
		$name = substr($thumbPath,0,$pos);		
		$extension = substr($thumbPath,$pos);
		return $name . '_large' . $extension;
	}
	
	
	function getListPaging($cPage, $pCount)
	{
		$listPaging = array(0,0,0,0,0);
		$startShowPage = 0;
		$startShowPage = ($pCount > 5) ? (($cPage > 3) ? $cPage - 2 : 1) : 1;
		if(($cPage + 2) > $pCount && $pCount > 5 )
		{
			$startShowPage -= ($cPage + 2) - $pCount;
		}
		$index = 0;
		$i=0;
		for($i=0; $i<5; $i++)
		{
			if(($startShowPage + $i) <= $pCount)
			{
				$listPaging[$index] = $startShowPage + $i;
				$index++;
			}
			else
			{
				break;
			}
		}
		return  $listPaging;
	}
	
	function getDomainFromUrl($strUrl)
	{
		$strUrl = trim($strUrl);
		if(trim($strUrl) == '') return '';
		$parse = parse_url($strUrl);
		$rel = '';
		if(isset($parse['host']))
		{
			$host = mb_strtolower($parse['host']);
			$pos = strpos($host, 'www.');
			if($pos !== false)
			{
				$rel = substr($host, $pos + 4);
			}
			else
			{
				$rel = $host;
			}
			
		}
		return $rel;
	}
	
	function validEmail($email){
		if(preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
			return true;
		else
			return false;
	}
	
	function validUsername($str) 
	{
	    return preg_match('/^([a-zA-Z])([a-z0-9_])*$/',$str);
	}
	
	function getLangCode()
	{
		$lang = '';
		$CI =& get_instance();
		if($CI->session->userdata('uname'))
		{
			$lang = $CI->session->userdata('lang');
			$lang = ($lang == '') ? $CI->config->item('language') : $lang;
		}
		else
		{
			if(get_cookie('lang', true))
			{
				$lang = get_cookie('lang', true);
				$lang = ($lang == '') ? $CI->config->item('language') : $lang;
			}
			else
			{
				$lang = $CI->config->item('language');
				set_cookie('lang', $lang);
			}
		}
		return $lang;
	}
	
	
	function validURL($url)
	{
		return (preg_match('/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? true : false;
	}
	
	function checkLogin()
	{
		$CI =& get_instance();
		if(!$CI->session->userdata('uname'))
		{
			//echo $CI->session->userdata('uname');
			$currUrl = '';
			if(isset($_GET['url']))
			{
				$currUrl = $_GET['url'];
				$currUrl = ($this->validURL($currUrl)) ? $currUrl : $CI->config->item('base_url');
			}
			else
			{
				$currUrl = $this->getCurrentUrl();
			}
			redirect(site_url('/login'));
			die();
		}
	}
	
	function getChartXAxis($maxValue)
	{
		$data = 0;
		if($maxValue < 1)
		{
			$data = 1;
		}
		else if($maxValue < 10)
		{
			$data = 10;
		}
		else
		{
			$numLen = strlen($maxValue);
			$step = pow(10, $numLen - 2);
			$tmp = $maxValue % $step;
			$tmp = $tmp;
			$tmp2 = ($maxValue - $tmp) / $step;
			$tmp2++;
			while($tmp2 % 5 > 0)
			{
				$tmp2++;
			}
			$data = $tmp2*$step;
		}
		return $data;
	}
	
	function getBannerImagPath($year, $month, $filename, $timeStemp = 0)
	{		
		if($timeStemp == 0)
		{
			return FTP_IMG_DOMAIN . "ssvimg/$year/$month/$filename";
		}
		else
		{
			$createdTimeInfo = getdate($timeStemp);
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			return FTP_IMG_DOMAIN . "ssvimg/$yearFolder/$monthFolder/$filename";
		}
	}
	
	function sendmail($data)
	{
		//Send mail
		$config['mailtype'] = 'html';
		$ci = & get_instance();
		$ci->load->library('email', $config);
				
		$mail_from = $ci->config->item('serving_email');
		$from_name = $ci->config->item('serving_email_name');
		$from_name = $from_name['vietnam'];
				
    	$ci->email->from($mail_from, $from_name);
		$ci->email->to($data['to']);
		if(array_key_exists('cc', $data)) $ci->email->cc($data['cc']);
		if(array_key_exists('bcc', $data)) $ci->email->bcc($data['bcc']);
		$ci->email->subject($data['subject']);
		$ci->email->message($data['body']);
		if(SENDMAIL) $ci->email->send();
		$ci->email->clear();
		//-->
	}
	
	function checkLocation($listId, $arrayLocation)
	{
		if(trim($listId) == '') return '';
		$out = explode(',', $listId);
		for($i = 0; $i < count($out); $i++)
		{
			$out[$i] = trim($out[$i]);
			if($out[$i] != '' && is_numeric($out[$i]))
			if(array_key_exists($out[$i], $arrayLocation)) $out[$i] = $arrayLocation[$out[$i]];
		}
		return implode(', ', $out);
	}
}

function show_custom_error($mess = '')
{
	$CI =& get_instance();
	if (class_exists('CI_DB') AND isset($CI->db))
	{
		$CI->db->close();
	}
	die($mess);
}

function getContentFromTextAraUseTinyMce($str)
{
	if(get_magic_quotes_gpc())
	{
		return $str;
	}
	else
	{
		$CI = &get_instance();
		return mysqli_real_escape_string($CI->db->conn_id, $str);
	}
}

function is_date($date)
{
    $date = str_replace(array('\'', '-', '.', ','), '/', $date);
    $date = explode('/', $date);

    if(    count($date) == 1 // No tokens
        and    is_numeric($date[0])
        and    $date[0] < 20991231 and
        (    checkdate(substr($date[0], 4, 2)
                    , substr($date[0], 6, 2)
                    , substr($date[0], 0, 4)))
    )
    {
        return true;
    }
   
    if(    count($date) == 3
        and    is_numeric($date[0])
        and    is_numeric($date[1])
        and is_numeric($date[2]) and
        (    checkdate($date[0], $date[1], $date[2]) //mmddyyyy
        or    checkdate($date[1], $date[0], $date[2]) //ddmmyyyy
        or    checkdate($date[1], $date[2], $date[0])) //yyyymmdd
    )
    {
        return true;
    }
    return false;
}
function check_permit_id($userid,$permitid,$groupid,$id,$typeid)
{
    if(($id<0)&&(!is_numeric($id))){
        return false;
    }else{
        $CI =& get_instance();
        $CI->load->model('bannerdetail/bannerdetail_model');
        $data = $CI->bannerdetail_model->selfserving_check_permit_by_user($userid,$permitid,$groupid,$id,$typeid);
        if($data['0']['0']==1){
            return true;
        }else{ 
            return false;
        }
    }
}
?>